package com.nf.practice.streamlambdatest;

import com.alibaba.fastjson.JSONObject;
import com.nf.practice.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class StreamCases {

    @Test
    public void test() {
        String dateStr1 = "2023.10.11";
        String dateStr2 = "2023-10-11 11:12:12";

        SimpleDateFormat simpleFormat4 = new SimpleDateFormat("yyyy.MM.dd");
        SimpleDateFormat simpleFormat5 = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date1 = simpleFormat4.parse(dateStr1);
            Date date2 = simpleFormat5.parse(dateStr2);
            Calendar ca = Calendar.getInstance();
            ca.setTime(date2);
            ca.set(Calendar.HOUR_OF_DAY, 0);
            ca.set(Calendar.MINUTE, 0);
            ca.set(Calendar.SECOND, 0);
            ca.set(Calendar.MILLISECOND, 0);

            Date time = ca.getTime();
            if (time.equals(date1)) {
                System.out.println("日期相等了");
            }

            if (date1.equals(date2)) {
                System.out.println("日期不等了/=");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }



//        SimpleDateFormat simpleFormat2 = new SimpleDateFormat("yyyy.MM");
//        Pattern simpleFormat2Pattern = Pattern.compile("^\\d{4}\\.\\d{2}$");
//        String[] testDates = {"2023.07", "2023.12", "2024.01", "2023/12/01", "2023-12-01", "2345", "五六提议", "---"};
//        // 遍历测试数据并验证
//        for (String date : testDates) {
//            if (StringUtils.isBlank(date) || !simpleFormat2Pattern.matcher(date).matches()) {
//                System.out.println(date + "非yyyy.MM格式");
//                continue;
//            }
//            System.out.println("Is date format:" + date);
//        }


    }

    private boolean filterTimeBetween(SimpleDateFormat simpleFormat,Pattern formatPattern,Date startDate,Date endDate,String targetDateStr) {
        if (StringUtils.isBlank(targetDateStr) || !formatPattern.matcher(targetDateStr).matches()) {
            return false;
        }
        if (targetDateStr.equals("--") || targetDateStr.equals("长期")) {
            return false;
        }
        Date targetDate = parseDate(targetDateStr, simpleFormat);
        return (targetDate.after(startDate) || targetDate.equals(startDate)) && (targetDate.before(endDate) || targetDate.equals(endDate));
    }

    public static Date parseDate(String dateStr, DateFormat df) {
        if(StringUtils.isBlank(dateStr) || "--".equals(dateStr) || "长期".equals(dateStr) || "000000".equals(dateStr))
            return null;
        Date date = null;
        try {
            date = df.parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    @Test
    public void testGroupByList() {
        List<JSONObject> result = new ArrayList<>();
        JSONObject item = new JSONObject();
        result.add(item);
        item.put("H_NUMBER","1");
        item.put("QUERYORG","商业银行\"WA\"");
        item.put("QUERYREASON_NAME","贷款审批");
        item.put("QUERYDATE","2023.10.01");

        JSONObject item2 = new JSONObject();
        result.add(item2);
        item2.put("H_NUMBER","2");
        item2.put("QUERYORG","商业银行\"WA\"");
        item2.put("QUERYREASON_NAME","贷款审批");
        item2.put("QUERYDATE","2023.09.25");

        JSONObject item3 = new JSONObject();
        result.add(item3);
        item3.put("H_NUMBER","4");
        item3.put("QUERYORG","广州市有限责任公司");
        item3.put("QUERYREASON_NAME","贷款审批");
        item3.put("QUERYDATE","2023.09.01");

        JSONObject item4 = new JSONObject();
        result.add(item4);
        item4.put("H_NUMBER","3");
        item4.put("QUERYORG","有限责任公司");
        item4.put("QUERYREASON_NAME","保前审查");
        item4.put("QUERYDATE","2023.09.01");

//        Map<String, List<JSONObject>> map = result.stream().collect(Collectors.groupingBy(res -> res.getString("QUERYDATE")));
//        Set<String> set = map.keySet();
//        System.out.println(set);
        result.stream().map(obj->{
            obj.put("org_reason", "11");
            obj.put("querydateDate", "22");
            return obj;
        }).sorted(Comparator.comparing(o -> o.getDate("querydateDate"))).collect(Collectors.toList());//按时间排序

        System.out.println(result);
    }
}
